﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using System.IO;

namespace Runda.CMS.Portal.Mobile
{
    public partial class WorkRegister : BasePage
    {
        private const string departmentID = "03589d4f-d56a-4217-bb72-39c16e1e16ad";
        private string strsql = "select a.UserID,UserName+ case when IsManager =1 then '-组长' else '' end +'('+c.DepartmentName +')' as UserName from Cms_Sys_User as a inner join Cms_Sys_UserDepartment as b on a.UserID=b.UserID inner join Cms_Sys_Department c on c.DepartmentID = b.DepartmentID where c.DepartmentID in ( select DepartmentID from Cms_Sys_Department where ParentID = '" + departmentID + "') or c.DepartmentID = '" + departmentID + "'";

        public Guid RegisterID
        {
            get
            {
                if (hiddenRegisterID.Text == string.Empty) return Guid.Empty;
                else return new Guid(hiddenRegisterID.Text);
            }
            set
            {
                hiddenRegisterID.Text = value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {              
                RegisterDate.Value = DateTime.Now.Date.ToShortDateString();
                RegisterDate.Disabled = true;
                ExpectEndDate.Value = DateTime.Now.Date.ToShortDateString();
                ExpectEndTime.Value = DateTime.Now.ToShortTimeString();
                DataTable dt = new CommonBll().getDataFromSQL(strsql);
                for (int index = 0; index < dt.Rows.Count; index++)
                {
                    PerformPerson.Items.Add(new ListItem(dt.Rows[index]["UserName"].ToString(), dt.Rows[index]["UserID"].ToString()));
                }
                PerformPerson.SelectedValue = CurrentUserID.ToString();            

                if (Request.QueryString["RegisterID"] != null)
                {
                    RegisterID = new Guid(Request.QueryString["RegisterID"]);
                    SystemBll bll = new SystemBll();
                    WsmTaskRegisterEntity entity = new WsmTaskRegisterEntity();
                    if (RegisterID != Guid.Empty)
                    {
                        entity = bll.Get<WsmTaskRegisterEntity>(RegisterID);
                    }

                    TaskType.SelectedValue = entity.TaskType.ToString();
                    customerName.Value = new SystemBll().Get<CtrtCustomerEntity>(entity.CustomerID).CustomerName;
                    Comment.Text = entity.Comment;
                    customerID.Text = entity.CustomerID.ToString();
                    String instrumentName = new SystemBll().Get<WsmInstrumentInfoEntity>(entity.InstrumentID).instrumentName;
                    Instrument.Items.Add(new ListItem(instrumentName, entity.InstrumentID.ToString() + "|" + entity.InstrumentSerialNum));
                    Instrument.SelectedValue = entity.InstrumentID.ToString() + "|" + entity.InstrumentSerialNum;                                        
                    instrumentHidden.Text = entity.InstrumentID.ToString() + "|" + entity.InstrumentSerialNum;
                    ExpectEndTime.Value = entity.ExpectEndDate.ToString("HH:mm");
                    RegisterDate.Value = entity.RegisterDate.Date.ToShortDateString();
                    ExpectEndDate.Value = entity.ExpectEndDate.Date.ToShortDateString();
                    PerformPerson.SelectedValue = entity.PerformPerson.ToString();
                }
                Instrument.Items.Add(new ListItem("---------未知---------","-1"));
            }
        }

        protected void btnHidden_Click(object sender, EventArgs e)
        {          
            if (hiddenM.Value == "1")
            {
                SaveTask_Click(this, e);
            }
            if (customerID.Text != "")
            {
                Instrument.Items.Clear();            
                DataTable dt = new CommonBll().getDataFromSQL("select convert(varchar(50),a.InstrumentID)+'|'+b.InstrumentSerialNum as InstrumentID,a.instrumentName from Cms_Wsm_InstrumentInfo as a inner join Cms_Wsm_TaskRegister as b on a.InstrumentID = b.InstrumentID where b.CustomerID='" + customerID.Text + "' and  b.TaskStatus = 1 and b.TaskType = 1");
                Instrument.Items.Add(new ListItem("---------未知---------", "-1"));
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int index = 0; index < dt.Rows.Count; index++)
                    {
                        Instrument.Items.Add(new ListItem(dt.Rows[index]["InstrumentName"].ToString(), dt.Rows[index]["InstrumentID"].ToString()));                       
                    }                
                }
            }
        }

        protected void SaveTask_Click(object sender, EventArgs e)
        {
            WsmTaskRegisterEntity entity = new WsmTaskRegisterEntity();        
            SystemBll bll = new SystemBll();
            if (RegisterID != Guid.Empty)
            {
                entity = bll.Get<WsmTaskRegisterEntity>(RegisterID);
            }
            else
            {
                entity = new WsmTaskRegisterEntity();
                RegisterID = entity.RegisterID;
                entity.CreatedBy = CurrentUserID;
                entity.CreatedOn = DateTime.Now;
                entity.TaskStatus = 0;
                entity.Deleted = false;
            }
            string[] instrumentArray;
            if (instrumentHidden.Text != "" && instrumentHidden.Text != "-1")
            {
                instrumentArray = instrumentHidden.Text.Split(new char[] { '|' });

                try
                {
                    entity.InstrumentID = new Guid(instrumentArray[0]);
                    entity.InstrumentSerialNum = instrumentArray[1];
                }
                catch
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "saveandclose12", "alert(1);", true);
                    return;
                }
            }
            entity.Comment = Comment.Text.Trim();
            entity.CustomerID = new Guid(customerID.Text);
            if (ExpectEndDate.Value != null)
                entity.ExpectEndDate = System.Convert.ToDateTime(ExpectEndDate.Value + " " + ExpectEndTime.Value);

            entity.InstrumentType = new SystemBll().Get<CtrtInstrumentEntity>(entity.InstrumentID).categoryGUID.ToString();
            entity.PerformPerson = new Guid(PerformPerson.SelectedValue);
            if (RegisterDate.Value != null)
                entity.RegisterDate = System.Convert.ToDateTime(RegisterDate.Value);
            entity.TaskType = System.Convert.ToInt32(TaskType.SelectedValue);
            bll.InsertOrUpdate(entity);
            hiddenM.Value = "";
            ClientScript.RegisterStartupScript(this.GetType(), "saveandclose12", "window.location.href='WorkRegisterList.aspx'", true);
        }       
    }
}
